package com.example.demo.controller;

import com.example.demo.pojo.Dept;
import com.example.demo.pojo.Result;
import com.example.demo.service.DeptService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping(value="/depts",method = RequestMethod.GET)
    public Result findAllDepts(){
        List<Dept> list = deptService.findAllDepts(); //alt+ enter
        if(list==null)
        {
            return Result.error("出现错误");
        }
        return Result.success(list);
    }


    @RequestMapping(value="/depts/{id}",method = RequestMethod.DELETE)
    public Result delete( @PathVariable int id){

        int row = deptService.deleteDeptById(id); //alt+ enter
        if(row<1)
        {
            return Result.fail("出现错误");
        }
        return  Result.success(row);
    }

    @RequestMapping(value="/depts",method = RequestMethod.POST)
    public Result insert(@RequestBody Dept dept){
        int row = deptService.insertDept(dept); //alt+ enter
        if(row<1)
        {
            return Result.fail("出现错误");
        }
        return  Result.success(row);
    }

    @RequestMapping(value="/depts/{id}",method = RequestMethod.GET)
    public Result findID(@PathVariable int id){
        Dept dept = deptService.findDeptById(id); //alt+ enter
        if(dept==null)
        {
            return Result.fail("出现错误");
        }
        return  Result.success(dept);
    }

    @PutMapping(value="/depts")
    public Result updateDept(@RequestBody Dept dept){
        int row=deptService.updateDept(dept);
        if(row<1)
        {
            return Result.fail("出现错误");
        }
        return Result.success(row);
    }
}
